var ACTION = require("../../base/action");
var utils = require("../../newUtils/utils");
var time = require("../../newUtils/time");
var ocrGml = require("../../newUtils/ocrGml");
var common = require("../common");
var eatAction = new ACTION("吃饭");

eatAction.goStart = function () {
  if (common.inHome()) {
    sleep(utils.getRandom(1.5, 2, 1000));
    common.toTask();
    sleep(utils.getRandom(1.5, 2, 1000));
    var ocrItem = ocrGml.captureExistOne("吃饭补贴");
    console.log(this.name, "goStart", (ocrItem ? "" : "没") + "找到-吃饭补贴-");
    if (ocrItem) {
      utils.clickOcrItem(ocrItem);
      sleep(utils.getRandom(2.5, 3, 1000));
    }
    return ocrItem;
  }
};
eatAction.beforeExec = function () {
  var ocrItem = ocrGml.captureExistOne("领取晚餐补贴");
  console.log((ocrItem ? "" : "没") + "找到-领取晚餐补贴-");
  return ocrItem;
};
eatAction.exec = function (ocrItem) {
  utils.clickOcrItem(ocrItem);
  sleep(utils.getRandom(2, 3, 1000));
  var contents = [
    //prettier-ignore
    "看视频再",
    "看广告再",
  ];
  var UIText = utils.existsOneTextStartsWith(contents);
  if (UIText) {
    console.log("找到-" + UIText.text() + "-");
    utils.clickUIText(UIText);
    sleep(utils.getRandom(2, 3, 1000));
    common.ad.init(1, function () {
      var ocrItem = ocrGml.captureExistOne([
        "时间到啦",
        "开饭时间",
        "每餐可获得",
      ]);
      console.log("找到-" + ocrItem.text + "-");
      return ocrItem;
    });
    return;
  }
  var contents = [
    //prettier-ignore
    "开心收下",
    "立即收下",
  ];
  var UIText = utils.existsOneTextStartsWith(contents);
  if (UIText) {
    console.log("找到-" + UIText.text() + "-");
    utils.clickUIText(UIText);
    return;
  }
};
eatAction.afterExec = function () {
  console.log(this.name, time.str(), "afterExec");
  sleep(utils.getRandom(1.5, 2, 1000));
  back();
};
module.exports = eatAction;
